package control;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.TreeSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modele.Dep;
import modele.Med;
import modele.Pays;

/**
 *
 * @author sio
 */
public class control extends HttpServlet {

    private Pays p;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        p = new Pays();
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String uc = request.getParameter("uc");
        String page = null;
        switch (uc){
            case "dep":
                String numDep = request.getParameter("numDep");
                if (numDep == null) {
                    Collection<Dep> d = p.getLesDeps();
                    request.setAttribute("lesDeps", d);

                    page = "index.jsp";
                } else {
                    Collection<Med> m = p.getLeDep(numDep).getLesMeds();
                    request.setAttribute("lesMeds", m);
                    page = "medParDep.jsp";
                }
                break;
                
            case "nom":
                String nomMed = request.getParameter("nomMed");
                if (nomMed == null) {
                    page = "medParNom.jsp";
                } else {
                    Collection<Dep> lesDeps = p.getLesDeps();
                    Collection<Med> lesMeds = new TreeSet<Med>();
                    for (Dep d : lesDeps) {
                        for (Med m : d.getLesMeds()) {
                            if (m.getNom().startsWith(nomMed)) {
                                lesMeds.add(m);
                            }
                        }
                    }
                    request.setAttribute("lesMeds", lesMeds);
                    page = "resMedParNom.jsp";
                }
                break;
                
            case "spe":
                String nomSpe = request.getParameter("specialitecomplementaire");
                if (nomSpe == null) {
                    Collection<Dep> d = p.getLesDeps();
                    request.setAttribute("lesDeps", d);

                    page = "index.jsp";
                } else {
                    Collection<Med> m = p.getLeDep(nomSpe).getLesMeds();
                    request.setAttribute("lesMeds", m);
                    page = "resMedParSpe.jsp";
                }
            }
//        if (uc.equals("dep")) {
//            String numDep = request.getParameter("numDep");
//
//            if (numDep == null) {
//                Collection<Dep> d = p.getLesDeps();
//                request.setAttribute("lesDeps", d);
//
//                page = "index.jsp";
//            } else {
//                Collection<Med> m = p.getLeDep(numDep).getLesMeds();
//                request.setAttribute("lesMeds", m);
//                page = "medParDep.jsp";
//            }
//        } else {
//            String nomMed = request.getParameter("nomMed");
//            if (nomMed == null) {
//                page = "medParNom.jsp";
//            } else {
//                Collection<Dep> lesDeps = p.getLesDeps();
//                Collection<Med> lesMeds = new TreeSet<Med>();
//                for (Dep d : lesDeps) {
//                    for (Med m : d.getLesMeds()) {
//                        if (m.getNom().startsWith(nomMed)) {
//                            lesMeds.add(m);
//                        }
//                    }
//                }
//                request.setAttribute("lesMeds", lesMeds);
//                page = "resMedParNom.jsp";
//            }
//        }
        request.getRequestDispatcher(page).forward(request, response);

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
